package com.tlvip.arithmetic.ch02;

/**
 * <p>description：二维数组内存模型，二维数组寻址</p>
 * <p>copyright： copyright(C)2016-2099</p>
 * <p>Life is short,we need passion</p>
 * <p>Summary： </p>
 * <p>instructions： </p>
 * Date 1/23/21</p>
 * Author mac
 *
 * @version 1.0
 */
public class DoubleArrayTest {
    // 行数
    private int rowSize = 0;
    // 列数
    private int colSize = 0;
    // 数据
    private int data[][];
    // 最大行数
    private int maxRow = 0;
    // 最大列数
    private int maxCol = 0;

    public DoubleArrayTest(int rowSize, int colSize) {
        this.rowSize = rowSize;
        this.colSize = colSize;
        this.data = new int[rowSize][colSize];
    }

    private void insert(int row, int col, int n) {
        for (int i = colSize - 1; i > col; i--) {
            data[row][i] = data[row][i - 1];
        }
        data[row][col] = n;
    }

    private void delete(int row, int col) {
        for (int i = col; i < colSize; i++) {
            if (i != colSize - 1) {
                data[row][i] = data[row][i + 1];
            } else {
                data[row][i] = 0;
            }
        }
    }

    private void update(int row, int col, int n) {
        data[row][col] = n;
    }

    private int get(int row, int col) {
        return data[row][col];
    }

    private void print() {
        int row, col;
        for (row = 0; row < rowSize; row++) {
            for (col = 0; col < colSize; col++) {
                System.out.print(row + "-" + col + ":" + data[row][col] + "  ");
            }
            System.out.println("");
        }
    }

    public static void main(String[] args) {
        DoubleArrayTest doubleArrayTest = new DoubleArrayTest(2, 3);
        doubleArrayTest.insert(0, 1, 4);
        doubleArrayTest.insert(1, 2, 5);
        doubleArrayTest.print();
        doubleArrayTest.delete(0, 1);
        doubleArrayTest.print();
        doubleArrayTest.update(0, 1, 9);
        doubleArrayTest.print();
        System.out.println(doubleArrayTest.get(1, 2));
    }
}
